package com.lyd.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.lyd.dao.FileDao;
import com.lyd.entity.FileEntity;

public class FileDaoImpl implements FileDao {

	@Override
	public boolean addFile(FileEntity nodeName) {
		Connection conn = DOBC.getConn();
		String sql = "insert into table_file(node_code,node_name,node_type,parent_node_code)values(?,?,?,?)";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1,nodeName.getNodeCode() );
			ps.setString(2, nodeName.getNodeName());
			ps.setString(3,nodeName.getNodeType());
			ps.setString(4, nodeName.getParentNodeCode());
			ps.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DOBC.close(null, ps, conn);
		return false;
	}

	@Override
	public boolean updateFile(FileEntity nodeName) {
		Connection conn=DOBC.getConn();
		String sql="update table_file set node_code=? where node_name=?";
		PreparedStatement ps=null;
		try {
			ps=conn.prepareStatement(sql);
			ps.setString(1, nodeName.getNodeCode());
			ps.setString(2, nodeName.getNodeName());
			ps.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		DOBC.close(null, ps, conn);
		return false;
	}

	@Override
	public boolean deleteFile(String nodeName) {
		Connection conn = DOBC.getConn();
		String sql = "update table_file set delete_flag=? where node_name = ?";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1,"Y");
			ps.setString(2,nodeName);
			ps.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DOBC.close(null, ps, conn);
		return false;
	}



	@Override
	public FileEntity get(String node_name) {
		String sql = "select node_name,node_code,node_type,parent_node_code from table_file where node_name='" + node_name+"'";
		FileEntity fileEntity = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		Connection conn = DOBC.getConn();
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			if (rs.next()) {
				fileEntity = new FileEntity();
				fileEntity.setNodeName(rs.getString(1));
				fileEntity.setNodeCode(rs.getString(2));
				fileEntity.setNodeType(rs.getString(3));
				fileEntity.setParentNodeCode(rs.getString(4));
				return fileEntity;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DOBC.close(rs, ps, conn);
		}
		return fileEntity;
	
	}

	@Override
	public List<FileEntity> listAll(String name) {
		String sql = "Select * from table_file where node_name like ?";
		Connection conn = DOBC.getConn();
		List<FileEntity> list = new ArrayList();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1,"%"+name+"%");
			rs = ps.executeQuery();
			// rs结果集
			while (rs.next()) {
				FileEntity fileEntity = new FileEntity();
				fileEntity.setNodeName(rs.getString(1));
				fileEntity.setNodeCode(rs.getString(2));
				
				// 循环添加到List
				list.add(fileEntity);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DOBC.close(rs, ps, conn);
		return list;
	}

	@Override
	public List<FileEntity> listAll() {
		// TODO Auto-generated method stub
		return null;
	}


}
